package  com.zh.java;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/*读取数据库属性文件,获取数据库连接信息*/
//如何让用户只能创建一个ConfigManager?单列模式:(1)把构造方法私有:(2)程序提供给别人唯一对象
//ConfigManager.getInstance().getString("jdbc.Driver");
//ConfigManager.getInstance().getString("jdbc.connection.url")

public class ConfigManager {
	private static  ConfigManager configmanager;
	private	Properties properties ;
	
	private ConfigManager(){
		String configFile="jdbc.properties";
		InputStream in = ConfigManager.class.getClassLoader().getResourceAsStream(configFile);
		try {
			properties = new Properties();
			properties.load(in);
			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	//提供给别人一个唯一的ConfigManager对象
	public static ConfigManager getInstance(){
		if(configmanager==null){
			configmanager=new ConfigManager();
		}
		return configmanager;
	}
	
	//根据属性文件中的键获得对应的值
	public String getString(String key){
		return properties.getProperty(key);
	}
	
	public static void main(String[] args) {
		ConfigManager configManager = ConfigManager.getInstance();
		System.out.println(configManager.getString("jdbc.url"));
		System.out.println(configManager.getString("jdbc.url"));
	}
}
